<?php
/**
 * Created by PhpStorm.
 * User: Liulibao
 * Date: 2019/10/17
 * Time: 16:36
 */

namespace app\api\controller;


use app\api\BaseController;
use app\common\model\AfficheModel;
use app\common\model\AfficheReadModel;
use think\Request;

/**
 * @title 公告管理
 */
class Affiche extends BaseController
{

    public function _initialize()
    {
        parent::_initialize();
//        Lang::load(APP_PATH . 'home/lang/'.config('default_lang').'/cart.lang.php');

    }

    /**
     * @title 获取首页公告的数量
     * @description
     * @author MrLiu
     * @url /Affiche/notices
     * @method GET
     *
     * @code 200 成功
     * @code 201 失败
     *
     * @return {"code":200,"msg":"请求数据成功","data": {'read_num':2}}
     */
    public function notices(AfficheModel $afficheModel, AfficheReadModel $afficheReadModel)
    {
        if ($afficheModel->counts() <= 0 ) {
            $read_num = 0;
        } else {
            $read_num = $afficheModel->counts() - $afficheReadModel->getCountByMemberId($this->user_id);
        }

        return json_success_return(compact('read_num'));
    }

    /**
     * @title 获取公告列表
     * @description
     * @author MrLiu
     * @url /Affiche/lists
     * @method GET
     *
     * @code 200 成功
     * @code 201 失败
     *
     * @param string page 分页1，2，3
     * @return {"code":200,"msg":"请求数据成功","data": []}
     */
    public function lists(AfficheModel $afficheModel, AfficheReadModel $afficheReadModel)
    {
        $afficheList = $afficheModel->getList([], 'affiche_id, title, contents, created_at');
        $afficheReadList = $afficheReadModel->getListByMemberId($this->user_id);
        // 判断是否已读
        if (!empty($afficheList)) {
            $readList = [];
            if (!empty($afficheReadList)) {
                $readList = array_column($afficheReadList, 'affiche_id');
            }
            if (!empty($afficheList['data'])) {
                array_walk($afficheList['data'], function ($val, $key) use ($readList, $afficheList) {
                    if (empty($readList)) {
                        $afficheList['data'][$key]['is_read'] = 0;
                    } else {
                        if (in_array($val['affiche_id'], $readList)) {
                            $afficheList['data'][$key]['is_read'] = 1;
                        } else {
                            $afficheList['data'][$key]['is_read'] = 0;
                        }
                    }
                });
            }
        }

        return json_success_return($afficheList);
    }


    /**
     * @title 获取公告详情
     * @description
     * @author MrLiu
     * @url /Affiche/noticeDetail
     * @method GET
     *
     * @code 200 成功
     * @code 201 失败
     *
     * @param string affiche_id 公告ID
     * @return {"code":200,"msg":"请求数据成功","data":[]}
     */
    public function noticeDetail(AfficheModel $afficheModel, Request $request, AfficheReadModel $afficheReadModel)
    {
        $affiche_id = $request->get('affiche_id', 0);
        if (intval($affiche_id) <= 0) {
            return json_fail_return(lang('ds_param_error'));
        }

        $detail = $afficheModel->getOneAfficheById($affiche_id);
        $data = array('member_id' => $this->user_id, 'affiche_id' => $affiche_id);
        // 更新阅读情况
        $afficheReadModel->createData($data, $data);
        return json_success_return($detail);
    }
}